using System;
using System.Collections.Generic;

namespace Mvvm.Models;

public class LinguisticAnalysisService
{

        public double SimplicityScore { get; set; }
        public double DirectnessScore { get; set; }
        public double EmotionalExpressivenessScore { get; set; }
        public double InteractionScore { get; set; }
        
        public int flag{ get; set; } 
        
        public string user1Name { get; set; }
        public string user2Name { get; set; }
        public string stringReport { get; set; }
        
        public LinguisticAnalysisService(ChatProcessedData chatData, int flag)
        {
            this.flag = flag;
            // 计算简洁性 vs. 复杂性
            SimplicityScore = CalculateSimplicity(chatData);

            // 计算直接性 vs. 间接性
            DirectnessScore = CalculateDirectness(chatData);

            // 计算情感表达性
            EmotionalExpressivenessScore = CalculateEmotionalExpressiveness(chatData);

            // 计算互动性
            InteractionScore = CalculateInteraction(chatData);
            user1Name = chatData.Username1;
            user2Name = chatData.Username2;
            
        }

        // 简洁性 vs. 复杂性：通过计算平均句子长度和句子复杂性（如从句、并列句）
        private double CalculateSimplicity(ChatProcessedData chatData)
        {
            // 假设每条消息的平均字数（简洁性指标：字数越少简洁，字数多则复杂）
            double user1AvgWordCountPerMessage = (double)chatData.User1WordCount / chatData.User1MessageCount;
            double user2AvgWordCountPerMessage = (double)chatData.User2WordCount / chatData.User2MessageCount;
            // 如果平均字数少于10则说明简洁，如果超过10则说明较复杂
            if (flag != 1)
                return user1AvgWordCountPerMessage ;
            return user2AvgWordCountPerMessage ;
        }

        // 直接性 vs. 间接性：通过分析肯定句（直接）和条件句/疑问句（间接）
        private double CalculateDirectness(ChatProcessedData chatData)
        {
            // 通过对高频词的分析，假设“吗”字多的为间接性表达
            double user1IndirectPhrases = chatData.User1HighFrequencyWords.Contains("吗") ? 10 : 0;
            double user2IndirectPhrases = chatData.User2HighFrequencyWords.Contains("吗") ? 10 : 0;

            // 如果有高频疑问词“吗”说明倾向间接
            if (flag != 1)
                return user1IndirectPhrases > 0 ? 10 : user1IndirectPhrases+10;
            return user2IndirectPhrases > 0 ? 10 : user2IndirectPhrases+10;
        }

        // 情感表达性：通过情感词汇（如“开心”，“生气”）的使用频率来判断
        private double CalculateEmotionalExpressiveness(ChatProcessedData chatData)
        {
            double user1Emotions = 100*(chatData.User1HappyMessageCount + chatData.User1LoveMessageCount
            + chatData.User1AngryMessageCount + chatData.User1SadMessageCount)/chatData.User1MessageCount;
            double user2Emotions = 100*(chatData.User2HappyMessageCount + chatData.User2LoveMessageCount
            + chatData.User2AngryMessageCount + chatData.User2SadMessageCount)/chatData.User2MessageCount;
            if (flag != 1)
            // 情感表达性：情感越丰富，得分越高
                return user1Emotions;
            return user2Emotions;
        }

        // 互动性：通过提问次数和回复时间等指标计算
        private double CalculateInteraction(ChatProcessedData chatData)
        {
            double User1AvgReplyTime =chatData.User1AverageReplyTime ;
            double User2AvgReplyTime =chatData.User2AverageReplyTime ;
            // 互动性较高的用户回复快速且频繁提问
            if (flag != 1)
                return  User1AvgReplyTime ;
            return User2AvgReplyTime;
        }

        public List<double> GenerateLinguisticReport()
        {
            stringReport = flag==0 ? $"                    {user1Name}人物语言风格报告\n" : $"                    {user2Name}人物性格报告\n";
            stringReport     += 
                           "------------------------------------------------\n" +
                           $"1. 简洁性 vs. 复杂性（Simplicity）：{SimplicityScore:F2}\n" +
                           $"   描述：该人物的语言{(SimplicityScore > 10 ? "简洁" : "复杂")}，倾向于使用短句和简明的表达方式。\n" +
                           $"2. 直接性 vs. 间接性（Directness）：{DirectnessScore:F2}\n" +
                           $"   描述：该人物的语言风格较{(DirectnessScore > 50 ?"直接" : "间接")}，更倾向于使用肯定句和明确表达。\n" +
                           $"3. 情感表达性（Emotional Expressiveness）：{EmotionalExpressivenessScore:F2}\n" +
                           $"   描述：该人物的语言{(EmotionalExpressivenessScore > 50 ? "富有情感" : "情感表达较少")}，偏向积极情感的表达。\n" +
                           $"4. 互动性（Interaction）：{InteractionScore:F2}\n" +
                           $"   描述：该人物的语言{(InteractionScore < 60 ? "互动性强" : "互动性一般")}，更倾向于提问和快速回应。\n" +
                           "------------------------------------------------\n";
            return new List<double>() { SimplicityScore, DirectnessScore, EmotionalExpressivenessScore, InteractionScore};
        }
    }
